﻿// RealTimeView.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "RealTimeView.h"
#include "CAutoStartDlg.h"

#define RTVIEW_BT_WIDTH 80
#define RTVIEW_BT_HEIGHT 40
// CRealTimeView

IMPLEMENT_DYNCREATE(CRealTimeView, CView)

CRealTimeView::CRealTimeView()
{
	m_nTimeSpan = 0;
	m_nTimer = 0;
	m_bRTMode = TRUE;
	m_nCurIndex = 0;
	m_bFirstStart = TRUE;
}

CRealTimeView::~CRealTimeView()
{
}

BEGIN_MESSAGE_MAP(CRealTimeView, CView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(BT_RUN_ID, &CRealTimeView::OnBnClickedRun)
	ON_WM_TIMER()
	ON_BN_CLICKED(BT_REALTIME_ID, &CRealTimeView::OnBnClickedRT)
	ON_BN_CLICKED(BT_NEXT_ID, &CRealTimeView::OnBnClickedNext)
	ON_BN_CLICKED(BT_LAST_ID, &CRealTimeView::OnBnClickedLast)
END_MESSAGE_MAP()


// CRealTimeView 绘图

void CRealTimeView::OnDraw(CDC* pSDC)
{
	int iDpiX = GetDeviceCaps(pSDC->m_hDC, LOGPIXELSX);
	int iDpiY = GetDeviceCaps(pSDC->m_hDC, LOGPIXELSY);
    g_DPIScaleX = (float)iDpiX / USER_DEFAULT_SCREEN_DPI;
    g_DPIScaleY = (float)iDpiY / USER_DEFAULT_SCREEN_DPI;

	CRect rect;
	GetClientRect(rect);
	CBitmap*Oldmap;
	CBitmap cBitmap;
	CDC memDC;
	cBitmap.CreateCompatibleBitmap(pSDC, rect.Width(), rect.Height());
	memDC.CreateCompatibleDC(pSDC);
	Oldmap = memDC.SelectObject(&cBitmap);
	CDC* pDC = &memDC;
	OnPrepareDC(pDC);

	Draw(pDC);

	pSDC->SetViewportOrg(0, 0);
	pSDC->SetWindowOrg(0, 0);
	pSDC->SetMapMode(MM_TEXT);

	pDC->SetViewportOrg(0, 0);
	pDC->SetWindowOrg(0, 0);
	pDC->SetMapMode(MM_TEXT);

	CRect UpdateRect;
	pSDC->GetClipBox(UpdateRect);

	pSDC->BitBlt(UpdateRect.left, UpdateRect.top,
		rect.Width(), rect.Height(), pDC,
		UpdateRect.left, UpdateRect.top, SRCCOPY);
	memDC.SelectObject(Oldmap);
	memDC.DeleteDC();
}


// CRealTimeView 诊断

#ifdef _DEBUG
void CRealTimeView::AssertValid() const
{
	CView::AssertValid();
}

#ifndef _WIN32_WCE
void CRealTimeView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif
#endif //_DEBUG


// CRealTimeView 消息处理程序
void	CRealTimeView::OnBnClickedRun()
{
	if (m_bFirstStart)
	{
		m_bFirstStart = FALSE;
		gl.m_nMachineStatus = 1;
		WriteLog(_T("设备自动启动流程开始..."));
		CAutoStartDlg* dlg = new CAutoStartDlg(this);
		dlg->DoModal();
		delete dlg;
	}
	else
	{
		gl.m_FireNumA = 0;
		gl.m_FireNumB = 0;
		gl.m_FireNumC = 0;

		gl.m_strNewMsg1 = gl.m_strNewMsg2 = _T("");
		gl.m_nMachineStatus = 2;
		if (gl.m_nWorkMode == 3)
			cControl.OpenRelay3(FALSE);
		else
			cControl.OpenRelay3(TRUE);

		gl.m_bTwoChCalibreate = FALSE;
		gl.m_bRun = !gl.m_bRun;
		if (m_nTimer != 0)
		{
			KillTimer(m_nTimer);
			m_nTimer = 0;
		}
		if (gl.m_bRun)
		{
			WriteLog(_T("启动分析---------------"));
			m_btRUN.SetWindowText(_T("停止分析"));
			gl.curChromA.Init();
			gl.curChromB.Init();
			if (gl.m_nWorkMode != 2)	//0--双通道；1--通道A;3--差分；
			{
				gl.m_bRunA = TRUE;
				cControl.OpenSwitch(0);
				if (gl.m_nWorkMode == 0 || gl.m_nWorkMode == 3)
					m_nTimer = SetTimer(9002, 1000, NULL);
			}
			else
			{
				gl.m_bRunB = TRUE;
				cControl.OpenSwitch(1);
			}
		}
		else
		{
			WriteLog(_T("结束分析---------------"));
			m_btRUN.SetWindowText(_T("开始分析"));
			gl.m_bRunA = gl.m_bRunB = FALSE;				
		}
	}
}

//断网重连之后的再运行
void	CRealTimeView::ReStartRun()
{
	if (gl.m_nWorkMode == 3)
		cControl.OpenRelay3(FALSE);
	else
		cControl.OpenRelay3(TRUE);

	gl.m_bTwoChCalibreate = FALSE;
	if (m_nTimer != 0)
	{
		KillTimer(m_nTimer);
		m_nTimer = 0;
	}
	if (gl.m_bRun)
	{
		m_btRUN.SetWindowText(_T("停止分析"));
		gl.curChromA.Init();
		gl.curChromB.Init();
		if (gl.m_nWorkMode != 2)	//0--双通道；1--通道A;3--差分；
		{
			gl.m_bRunA = TRUE;
			cControl.OpenSwitch(0);
			if (gl.m_nWorkMode == 0 || gl.m_nWorkMode == 3)
				m_nTimer = SetTimer(9002, 1000, NULL);
		}
		else
		{
			gl.m_bRunB = TRUE;
			cControl.OpenSwitch(1);
		}
	}
}

//实时模式
void	CRealTimeView::OnBnClickedRT()
{
	m_bRTMode = !m_bRTMode;
	if (m_bRTMode)
	{
		m_btRT.SetWindowTextW(_T("实时"));
		m_btRT.SetIcon(IDI_RT, NULL);
	}
	else
	{
		m_btRT.SetWindowTextW(_T("历史"));
		m_btRT.SetIcon(IDI_HIS, NULL);
	}

}

void	CRealTimeView::OnBnClickedLast()
{
	m_bRTMode = FALSE;
	m_btRT.SetWindowTextW(_T("历史"));
	if (gl.m_lstChromFiles.GetCount() == 0) return;
	m_nCurIndex--;
	if (m_nCurIndex < 0)
		m_nCurIndex = 0;
	CHROM_LIST cl = gl.m_lstChromFiles.GetAt(m_nCurIndex);
	Chrom_File cf = gl.ReadChmDate(cl.strFileName);
	m_wndLastCurve.UpdateGraph(cf, cl.nCH);
	m_wndIntResult.UpdateGrid(cf);
}

void	CRealTimeView::OnBnClickedNext()
{
	m_bRTMode = FALSE;
	m_btRT.SetWindowTextW(_T("历史"));
	if (gl.m_lstChromFiles.GetCount() == 0) return;
	m_nCurIndex++;
	if (m_nCurIndex >= gl.m_lstChromFiles.GetCount())
		m_nCurIndex = m_nCurIndex - 1;
	CHROM_LIST cl = gl.m_lstChromFiles.GetAt(m_nCurIndex);
	Chrom_File cf = gl.ReadChmDate(cl.strFileName);
	m_wndLastCurve.UpdateGraph(cf, cl.nCH);
	m_wndIntResult.UpdateGrid(cf);
}

void	CRealTimeView::Draw(CDC* pDC)
{
	CRect rect;
	GetClientRect(rect);
	BxGradientFill(pDC, rect, BACKCOLOR1, BACKCOLOR2, 1);

}


int CRealTimeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	long Style;
	SetWindowLong(this->m_hWnd,GWL_EXSTYLE,0);
	CRect rect(0, 0, 10, 10);
	m_wndRTCurve.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, this, WND_REALTIMECURVE_ID, NULL);
	m_wndLastCurve.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, this, WND_LASTCUVE_ID, NULL);
	m_wndIntResult.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, this, WND_INTRESULT_ID, NULL);
	m_btNext.Create(_T("下一个"), WS_CHILD | WS_VISIBLE, rect, this, BT_NEXT_ID);
	m_btRT.Create(_T("实时"), WS_CHILD | WS_VISIBLE, rect, this, BT_REALTIME_ID);
	m_btLast.Create(_T("上一个"), WS_CHILD | WS_VISIBLE, rect, this, BT_LAST_ID);
	m_btRUN.Create(_T("启动分析"), WS_CHILD | WS_VISIBLE, rect, this, BT_RUN_ID);

	m_nScanTimer = SetTimer(9003, 1000, NULL);
	return 0;
}

#define RTCURVE_MOVE_TO_TOP 50
void CRealTimeView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	CRect rect;
	GetClientRect(rect);
	try
	{
		if (m_wndRTCurve.m_hWnd)
		{
			int nBtWidth=RTVIEW_BT_WIDTH*g_DPIScaleX;
			CRect rt = rect;
			rt.top = rect.Height() / 2- RTCURVE_MOVE_TO_TOP*g_DPIScaleY;
			m_wndRTCurve.MoveWindow(rt);

			rt = rect;
			rt.bottom = rect.Height() / 2 - 2- RTCURVE_MOVE_TO_TOP*g_DPIScaleY;
			rt.right = rect.Width() / 2 - nBtWidth / 2+10*g_DPIScaleX;
			m_wndLastCurve.MoveWindow(rt);
			rt.left = rect.Width() / 2 + nBtWidth / 2+10*g_DPIScaleX;
			rt.right = rect.right;
			m_wndIntResult.MoveWindow(rt);

			rt = rect;
			rt.left = rect.Width() / 2 - nBtWidth / 2 + 14*g_DPIScaleX;
			rt.right = rect.Width() / 2 + nBtWidth / 2 +6*g_DPIScaleX;
			rt.top = rect.Height() / 15;
			rt.bottom = rt.top + RTVIEW_BT_HEIGHT*g_DPIScaleY;
			m_btLast.MoveWindow(rt);
			rt.top = rect.Height() * 2 / 15;
			rt.bottom = rt.top + RTVIEW_BT_HEIGHT*g_DPIScaleY;
			m_btRT.MoveWindow(rt);
			rt.top = rect.Height() * 3 / 15;
			rt.bottom = rt.top + RTVIEW_BT_HEIGHT*g_DPIScaleY;
			m_btNext.MoveWindow(rt);
			rt.top = rect.Height() * 5 / 15;
			rt.bottom = rt.top + RTVIEW_BT_HEIGHT*g_DPIScaleY;
			m_btRUN.MoveWindow(rt);
		}
	}
	catch (...)
	{
	}
}


void CRealTimeView::OnInitialUpdate()
{
	CView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	CWnd *pWnd = GetTopWindow();
	while (NULL != pWnd)
	{
		pWnd->SetFont(&gl.m_pSmallFont);
		pWnd = pWnd->GetNextWindow();
	}
	m_btRT.SetIcon(IDI_RT, NULL);

	CRect rect;
    GetClientRect(&rect);
    PostMessage(WM_SIZE, (WPARAM)SIZE_RESTORED, MAKELPARAM(rect.Width(), rect.Height()));
}


HBRUSH CRealTimeView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CView::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何特性
	if (nCtlColor == CTLCOLOR_STATIC)
	{
		/*switch (pWnd->GetDlgCtrlID())
		{
		case IDC_TIPSINFO:
			pDC->SetTextColor(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
			break;
		}*/
		return gl.m_brBack1;
	}
	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}


void CRealTimeView::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_nTimer == nIDEvent )
	{
		m_nTimeSpan++;
		if (m_nTimeSpan == 15)		//保证在A通道启动15秒后启动B通道
		{
			KillTimer(m_nTimer);
			m_nTimer = 0;
			m_nTimeSpan = 0;
			gl.curChromB.Init();
			gl.m_bRunB = TRUE;
			cControl.OpenSwitch(1);
		}
	}

	if (m_nScanTimer == nIDEvent && m_bRTMode && gl.nReCaculate != 0 && !gl.m_bTwoChCalibreate)  //非两通道校准模式下
	{
		int nType = gl.nReCaculate;
		gl.nReCaculate = 0;
		switch (nType)
		{
		case 1:
			m_wndLastCurve.UpdateGraph(gl.lastChromA,0);
			m_wndIntResult.UpdateGrid(gl.lastChromA);
			break;
		case 2:
			m_wndLastCurve.UpdateGraph(gl.lastChromB,1);
			m_wndIntResult.UpdateGrid(gl.lastChromB);
			break;
		case 3:
			m_wndLastCurve.UpdateGraph(gl.lastChromA,1);
			m_wndIntResult.UpdateGrid(gl.lastChromA);
			break;
		}
	}

	if (m_nScanTimer == nIDEvent && m_bRTMode && !gl.m_bTwoChCalibreate)  //非两通道校准模式下
	{
		m_wndIntResult.UpdateSpecialData(gl.gcStatus.dFidCV,gl.m_dTGResult, gl.m_dCO2Result, gl.m_dLagDepth);
	}

	CView::OnTimer(nIDEvent);
}

void	CRealTimeView::RefreshBtStatus()
{
	if (gl.m_bTwoChCalibreate)
	{
		m_btRUN.SetWindowText(_T("开始分析"));
		gl.m_bRunA = gl.m_bRunB = FALSE;
		gl.m_bRun = FALSE;
	}
}